from django.http import JsonResponse
"""
定义 Mixin扩展类
实现 判断用户是否登陆
"""


# 3.定义装饰函数
def my_decorator(view):
    # 4.内层函数
    def wrapper(request, *args, **kwargs):
        # 5.判断用户是否登陆
        if request.user.is_authenticated:
            # 6.登陆
            print('登陆')
            return view(request, *args, **kwargs)

        else:
            # 7.未登陆
            return JsonResponse({'code': 400, 'errmsg': '请登录后重试'})
    return wrapper


# 1.继承object基类
class LoginRequiredMixin(object):

    # 2.重写as_view方法
    @classmethod
    def as_view(cls, **initkwargs):
        view = super().as_view(**initkwargs)
        return my_decorator(view)
